package com.evildog.workspace.file.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.file.bo.DocBO;
import com.evildog.workspace.file.bo.DocPageBO;
import com.evildog.workspace.file.convert.DocConvert;
import com.evildog.workspace.file.model.DocPageParam;
import com.evildog.workspace.file.model.DocParam;
import com.evildog.workspace.file.service.DocService;
import com.evildog.workspace.file.vo.DocPageVO;
import com.evildog.workspace.file.vo.DocVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * doc Controller
 * 存储文件
 * </p>
 *
 * @author evildog
 * @since 2020-07-19
 */
@RestController
@RequestMapping("/doc")
@AllArgsConstructor
@Api(tags = "doc相关接口")
public class DocController {
    private DocService docService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody DocParam docParam) {
        return ResultGenerator.status(docService.save(DocConvert.INSTANCE.convert(docParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(docService.removeById(id));
    }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "批量删除接口")
    public Result<Boolean> delete(@RequestParam("ids") List<Integer> ids) {
        return ResultGenerator.status(docService.removeByIds(ids));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @RequestBody DocParam docParam) {
        return ResultGenerator.status(docService.updateById(id, DocConvert.INSTANCE.convert(docParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<DocVO> getById(@PathVariable("id") Long id) {
        DocBO docBO = docService.getDetailById(id);
        return ResultGenerator.success(DocConvert.INSTANCE.convert(docBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<DocPageVO>> getPageList(DocPageParam docPageParam, PageQuery query) {
        PagedResult<DocPageBO> docPageBO = docService.getPageList(
                query,
                QueryConditionUtils.getQueryCondition(docPageParam),
                DocConvert.INSTANCE.convert(docPageParam));
        return ResultGenerator.success(DocConvert.INSTANCE.convert(docPageBO));
    }

}
